Kurt Hsu's blog

The Rails developer in taiwan.


  • 首頁

  • 標籤

  • 分類

  • 歸檔

[Rails]建立mailer和串接Mailgun發送郵件

發表於 2018-05-04 更新於 2020-05-17 分類於 Rails , mailer , Mailgun

參考文章:
Rails 實戰聖經
Rails Guides

前言


我們在創造Rails專案的時候其實已經有下列檔案:
app/mailsers/application_mailer.rb
app/views/mailer.html.erb
app/views/mailer.text.erb

基本上跟原本的網頁東西原理很像
app/mailsers裡面的東西就像是controller
app/views/mailer.html.erb就像是網頁的application.html.reb
app/views/mailer.text.erb則是因為不是所有客戶端都可以顯示 HTML 格式的信件所以有存文字檔

建立一個最簡單的mailer


$ rails generate mailer UserMailer
創造了:
app/mailers/user_mailer.rb
app/views/user_mailer
以及一些test檔

先修改主要的application_mailer檔案:

app/mailers/application_mailer.rb
1
2
3
4
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end

default為設定寄信人的信箱

再來創造一個最簡單的welcome_email

app/mailers/user_mailer.rb
1
2
3
4
5
6
class UserMailer < ApplicationMailer
def welcome_email(email)
@email = email
mail(to: @email, subject: 'Welcome to My Awesome Site')
end
end

這時候剛剛創造的method是什麼名稱就在app/views/user_mailer資料夾創造一樣名稱的html:
$ touch app/views/user_mailer/welcome_email.html.erb

簡單做出html:

app/views/user_mailer/welcome_email.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome!!!</h1>
<p>
You have successfully signed up here
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>

再來可以先嘗試在rails console看有沒有東西:
$ rails c
$ UserMailer.welcome_email("example@test.com")

此時如果真的用送出去的指令:
$ UserMailer.welcome_email("example@test.com").deliver!
則會報錯

由於在本地端永遠不會真的送出郵件,利用letter_opener這個gem可以模擬信件在遊覽器上面

Gemfile
1
gem 'letter_opener', group: :development

$ bundle install

修改config/environments/development.rb環境檔:

config/environments/development.rb
1
2
3
4
…(略)
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :letter_opener
end

$ rails c
$ UserMailer.welcome_email("example@test.com").deliver!
應該可以成功看到模擬畫面!

串接mailgun


我的專案是利用Capistrano部署在linnode上

註冊完mailgun後他會給一個免費的domain帶有亂碼sandboxdxxxx….mailgun.org,打開之後有以下訊息:

  • IP Address
  • SMTP Hostname
  • Default SMTP Login
  • API Base URL
  • Default Password
  • API Key

修改production.rb:

config/environments/production.rb
1
2
3
4
5
…(略)
config.action_mailer.default_url_options = { :host => '你的网域名称,暂时还没有就填IP地址' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = config_for(:email).symbolize_keys
end

mailer會需要mailer.yml這隻檔案,但為了避免自己的密碼被看到,創造一個空的檔案:
$ touch config/email.yml

然後修改deploy.rb檔案:

deploy.rb
1
append :linked_files, "config/database.yml", "config/secrets.yml", "config/email.yml"

代表說等等他會去伺服器上面路徑~/jdstore/shared/config/裡面找這些檔案

到伺服器上:
$ touch 專案名稱/shared/config/email.yml
輸入以下資訊:

email.yml
1
2
3
4
5
6
7
8
production:
address: "smtp.mailgun.org"
port: 587
domain: "mailgun.org"
authentication: "plain"
user_name: "postmaster@sandboxXXXXXX.mailgun.org"
password: "XXXXX"
enable_starttls_auto: true

user_name替換成mailgun上的Default SMTP Login
password替換成mailgun上的Default Password

回到本地端commit一版push到github上面後deploy
$ cap production deploy

# Rails # mailer # Mailgun
[Devops]letsencrypt申請https (Nginx in Ubuntu 16.04)
[Rails]Bootstrap制作漂亮flash
  • 文章目錄
  • 本站概要

Kurt Hsu

Progress One Percent Every Day
171 文章
55 分類
163 標籤
RSS
  1. 1. 前言
  2. 2. 建立一個最簡單的mailer
  3. 3. 串接mailgun
    1. 3.0.1. 我的專案是利用Capistrano部署在linnode上
© 2020 Kurt Hsu
由 Hexo 強力驅動 v3.8.0
|
主題 – NexT.Muse v7.3.0